import router from '@/router'
const modules = import.meta.glob('../**/**/*.vue')

export function getAsyncRoutes(routers,isFirst) {
    if (routers) {
     let routerArray=[];

    routers.filter((itemRouter) => {
     let isChild=  itemRouter.children && itemRouter.children.length > 0;
     //路由没进行处理 当子路由不存在时设置为false
     if(isChild === undefined){
        isChild=false;
     }
        const oRouter = {  path: itemRouter.path,
            component: (() => {
                // 第一路由
                if (isFirst) {
                   return  modules[`../page/index/index.vue`];   
                }else{
                    // 路由页面
                 return modules[`../views/${itemRouter.component}.vue`];   
                }
             })(),
             name:itemRouter.label,
             icon:itemRouter.icon,
             meta: {title:itemRouter.label},
             query: itemRouter.query, 
             redirect: (() => {
                if (!isChild && isFirst){
                    return `${itemRouter.path}`
                }else{
                    return '';
                }
              })(),
              // 判断子路由是否存在
              children:!isChild ? (() => {
                  if(isFirst){
                  return [{
                    component: modules[`../views/${itemRouter.component}.vue`],
                    icon: itemRouter.icon,
                    name: itemRouter.label,
                    meta: {title:itemRouter.label},
                    query:itemRouter.query,
                    path: ''
                  }]
                }
                return []
              })() : (() => {
                  // 存在递归调用 
                return getAsyncRoutes(itemRouter.children,false)
              })()}
      
              routerArray.push(oRouter)
      })
      if (isFirst) {
        routerArray.forEach((rot) => router.addRoute(rot))
      } else {
        return routerArray
      }
    }
  }

